package com.wahoyu.exportwordtest.task;

import com.wahoyu.exportwordtest.service.ExportWordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * 定时周期任务
 * @Author wanghongyu02
 * @Date 2025/4/25 10:07
 */
@Configuration
@EnableScheduling
@Slf4j
@ConditionalOnProperty(value = "scheduled.enabled", havingValue = "true")
public class ScheduledTask {

    // 测试用注解，开发环境是1，生产环境是0（默认是0）
    @Value("${devEnv.flag:0}")
    private int runningFlag;

    @Autowired(required = false)
    private ExportWordService exportWordService;

    /**
     * 定时任务
     */
    @PostConstruct
    @Scheduled(cron = "${scheduled.corn}")
    public void scheduledTask() throws IOException {
        if (runningFlag == 0) {
            log.info("定时任务开始执行,正式环境--------------------");
            exportWordService.exportAllWord(); // 导出所有客户运行报告
        } else {
            log.info("定时任务开始执行,测试环境--------------------");
            exportWordService.testExportWord(); // 导出单个客户运行报告
        }
        log.info("定时任务执行完毕--------------------");
    }
}